package com.five.controller;

import com.five.dto.CollectionDto;
import com.five.exception.BusinessException;
import com.five.result.Result;
import com.five.service.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/reader")
public class CollectionController {
    @Autowired
    private ICollectionService collectionService;

    /*
     * 新增收藏
     * */
    @PostMapping("/collect/add")
    public Result<Void> addCollect(@RequestHeader("token") String token, Long novelId) {
        try {
            collectionService.addCollect(token, novelId);
            return Result.success("收藏中...");
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /*
     * 取消收藏
     * */
    @PostMapping("/collect/cancel")
    public Result<Void> cancelCollect(@RequestHeader("token") String token, Long novelId) {
        try {
            collectionService.cancelCollect(token, novelId);
            return Result.success("已取消收藏");
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /*
     * 书架，查询用户收藏的小说ID列表
     * */
    @PostMapping("/bookshelf")
    public Result<Set<Long>> getReaderCollectNovelIds(@RequestHeader("token") String token) {
        try {
            Set<Long> readerCollectNovelIds = collectionService.getReaderCollectNovelIds(token);
            return Result.success(readerCollectNovelIds);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

}